package com.ghsc.ghxc.jsb.ui.integral.fragment

import android.os.Bundle
import android.text.TextUtils
import com.cn.appcore.base.BaseVMFragment
import com.cn.appcore.ext.gone
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.ext.startKtxActivity
import com.cn.appcore.ext.visible
import com.cn.appcore.utils.GlideUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.databinding.FragmentIntegralMyBinding
import com.ghsc.ghxc.jsb.ui.home.activity.GoodsDetailActivity
import com.ghsc.ghxc.jsb.ui.integral.IntegralSupermarketActivity
import com.ghsc.ghxc.jsb.ui.integral.IntegralSupermarketViewModel
import com.ghsc.ghxc.jsb.ui.integral.activity.ReleaseCommodityActivity
import com.ghsc.ghxc.jsb.ui.integral.activity.TabActivity
import com.ghsc.ghxc.jsb.ui.integral.bean.IntegralUserInfoData

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.integral.fragment
 * 类名：IntegralSupermarketMyFragment
 * 创建时间：2024/5/20 10:45
 * 创建人 @author： xiaolin
 * 描述 ：Todo...积分我的
 */
class IntegralSupermarketMyFragment : BaseVMFragment<IntegralSupermarketViewModel, FragmentIntegralMyBinding>(
    R.layout.fragment_integral_my){
    override fun attachVMClass(): Class<IntegralSupermarketViewModel>? {
        return  IntegralSupermarketViewModel::class.java
    }


    var userInfo: IntegralUserInfoData?= null


    override fun lazyLoad() {

    }


    override fun onResume() {
        super.onResume()
        mViewModel.onShopuserInfoMethod()
    }


    override fun initView(savedInstanceState: Bundle?) {

    }


    override fun initClickListener() {
        super.initClickListener()
        binding.btnPut.setSingleClickListener {
            //发布商品
            userInfo?.let {
                if (it.jifen < it.needjifen) {
                    showToast(if(!TextUtils.isEmpty(it.tips)) it.tips else "积分不足")
                } else {
                  //跳转发布商品页面
                    startKtxActivity<ReleaseCommodityActivity>()
                }
            }

        }

        binding.tvBtnPut.setSingleClickListener{
            //发布的
            startKtxActivity<TabActivity>(
                value =  "type" to 1
            )
        }
        binding.tvBtnSell.setSingleClickListener {
            //卖出的
            startKtxActivity<TabActivity>(
                value =  "type" to 2
            )
        }
        binding.tvBtnBuy.setSingleClickListener {
            //买到的
            startKtxActivity<TabActivity>(
                value =  "type" to 3
            )
        }
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@IntegralSupermarketMyFragment) {
                showToast(it)
            }
            againLoginMsg.observe(this@IntegralSupermarketMyFragment) {
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }

            onShopuserInfoData.observe(this@IntegralSupermarketMyFragment){
                userInfo=it.data.data
                GlideUtil.ImageHeadLoad(requireActivity(),it.data.data.headimg, binding.ivHeader)
                binding.tvName.text = it.data.data.nickname
                binding.tvPhone.text = "手机号：${it.data.data.mobile}"

                if(it.data.data.pubcnt>0){
                    binding.tvCountPut.text=it.data.data.pubcnt.toString()
                    binding.tvCountPut.visible()
                }else binding.tvCountPut.gone()

                if(it.data.data.buycnt>0){
                    binding.tvCountBuy.text=it.data.data.buycnt.toString()
                    binding.tvCountBuy.visible()
                }else binding.tvCountBuy.gone()

                if(it.data.data.salecnt>0){
                    binding.tvCountSell.text=it.data.data.salecnt.toString()
                    binding.tvCountSell.visible()
                }else binding.tvCountSell.gone()


                UserManager.saveIntegralUserInfo(it.data.data)
            }
        }
    }

}